#!/usr/bin/env python  
# encoding: utf-8   

""" 
@version: v1.0 
@author: null 
@software: PyCharm 
@file: main.py 
@time: 2017.3.17 22:59 
"""

import itchat
from itchat.content import *
import robot
import time
from logger_model import Logger

isOpen=True
myInfo={}
chatList=[]
chatedList=[]
autoText='忙'
groupChatList={}
isOpenGroup=False
log = Logger('./log/wx_logs.log')

@itchat.msg_register([TEXT,PICTURE,RECORDING])
def friendsMsg(msg):
    log.debug('个人消息----------------------')
    log.info(msg)
    msgText = msg['Text']
    msgUserName = msg['FromUserName']
    if msgUserName == myInfo['UserName']:
        global isOpen
        global isOpenGroup
        if msgText=="//startup":
            isOpen=True
            return '机器人已开启'
        if msgText=="//stop":
            isOpen=False
            return '机器人已关闭'
        if msgText.find('//[')==0 and msgText.find(']')==len(msgText)-1:
            global autoText
            autoText=msgText[3:-1]
            return '已设置自动回复内容为[ '+autoText+' ]'
        if msgText=='//startgroup':
            isOpenGroup=True
            return '群聊AI开启'
        if msgText=='//stopgroup':
            isOpenGroup=False
            return '群聊AI关闭'

    elif isOpen:
        if not msgUserName in chatList:
            if msgText.find('//openxianliao')!=-1:
                chatList.append(msgUserName)
                return '进入闲聊模式~来和我聊天吧。输入指令 //closexianliao  关闭闲聊模式'
            elif not msgUserName in chatedList:
                chatedList.append(msgUserName)
                locatime = time.localtime()
                timeHours = locatime[3]
                timeWeek = locatime[6]
        #        if 0<=timeWeek<5 and (9<=timeHours<=12 or 2<=timeHours<=17):
                return '我是机器人小若，老板在'+autoText+'，有啥事你留言。你还可以输入指令 //openxianliao  进入闲聊模式和我聊天'
        else:
            if msgText.find('//closexianliao')!=-1:
                chatList.remove(msgUserName)
                chatedList.remove(msgUserName)
                return '闲聊模式关闭，输入指令 //openxianliao 再次开启'
            else:
                return robot.get_response(msgText)
    pass


@itchat.msg_register(TEXT, isGroupChat=True)
def groupMsg(msg):
    log.debug('群消息----------------------')
    log.info(msg)
    if isOpen:
        #print(msg)
        FromUserName = msg['FromUserName']
        if not FromUserName==myInfo['UserName']:
            groupChat = itchat.search_chatrooms(userName=FromUserName)
            #print(groupChat)
            #print(groupChat['Statues'])#增加群是否为免打扰 0为免打扰
            isAt=msg['isAt']
            if isAt:
                return '我是机器人，BOSS在'+autoText+'，重要事情私聊留言给我。'

            if groupChat['NickName']=='雪玲脱单改群名' and isOpenGroup:
                robotinfo = robot.get_response(msg['Text'])
                log.info("图灵机器人"+robotinfo)
                return robotinfo
    pass

def login():
    itchat.auto_login(hotReload=True)
    global myInfo
    myInfo = itchat.search_friends()
    print(myInfo['UserName'])
    global groupChatList
    groupChatList = itchat.get_chatrooms(update=True)
    itchat.run()

    pass

if __name__ == "__main__":
    login()
    pass  
